<?php
class PaypalAction extends Action{
	public function checkout(){
		$uid = $this->_get('uid');
		
		if (empty($uid)) {
			$this->error(L('checkout_failed'));
		}
		
		$this->assign('uid', $uid);
		$this->assign('host', 'http://'.$_SERVER['HTTP_HOST']);
		$this->display();
	}
	public function notify() {
		$paper_id = $_POST['item_number'];
		$num_cart_items = $_POST['num_cart_items'];
		$status = $_POST['payment_status'];
		if ($status == "Completed") {
			//付费成功，在此情况下发货是安全的。
			$uid = decryptUid($paper_id);
			$Users = D('Users');
			$result = $Users->checkoutComplete($uid);
			if ($result !== false) {
				$userInfo = $Users->getBasicUserInfoByUid($uid);
				$Users->sendEmail($userInfo);
				//$this->redirect('/Users/login');
			}
		}
		elseif ($status == "Pending") {
			//款项在途，目前Paypal有可能出现状态为Pending，实际上已经支付成功的情况。
			echo $_POST['pending_reason'];
		}
		//输出$_POST的所有数据。
		foreach($_POST as $key => $value)
		{
			echo "POST Data: $key -> $value <br>";
		}
	}
	
	public function thanks() {
		$itemNumber = $this->_get('item_number');
		$this->assign('itemNumber', $itemNumber);
		$this->display();
	}
}

?>